﻿using DataScripter.Excel.Formulas;

namespace DataScripter.Excel.Functions
{
	internal class Lookup : Formula
	{
		private readonly Formulas.Table _lookupTable;
		private readonly int _columnIndex;
		private readonly string _cellReference;
		private readonly Formula _toLookupFormula;

		public Lookup(ICell current, Formulas.Table lookupTable, int columnIndex)
		{
			_lookupTable = lookupTable;
			_columnIndex = columnIndex;
			_cellReference = current.CellReference;
		}

		public Lookup(Formula toLookup, Formulas.Table lookupTable, int columnIndex)
		{
			_lookupTable = lookupTable;
			_columnIndex = columnIndex;
			_toLookupFormula = toLookup;
		}

		#region Overrides of Formula

		public override string Resolve(ICell current)
		{
			return string.Format(DutchFunctionNames.VLOOKUP + "({0};{1};{2})", _cellReference ?? _toLookupFormula.Resolve(current), _lookupTable, _columnIndex);
		}

		#endregion
	}
}